using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Web;

namespace System.Utility.Helper
{
	public class HttpUtils
	{
		public HttpUtils()
		{
		}

		public static string GetQueryString(string strParaName, string strUrl, bool urlDecode = false)
		{
			string str;
			char[] chrArray = new char[] { '&' };
			string[] strArrays = strUrl.Split(chrArray);
			int num = 0;
			while (true)
			{
				if (num >= (int)strArrays.Length)
				{
					str = "";
					break;
				}
				else if (strArrays[num].IndexOf(strParaName) < 0)
				{
					num++;
				}
				else
				{
					string str1 = strArrays[num];
					chrArray = new char[] { '=' };
					string str2 = str1.Split(chrArray)[1];
					str = (urlDecode ? HttpUtility.UrlDecode(str2) : str2);
					break;
				}
			}
			return str;
		}

		public static string SendSyncRequest(string url, string queryString)
		{
			string str = SendSyncRequest(url, queryString, EnumHttpMode.Get, Encoding.Default, "", "");
			return str;
		}

		public static string SendSyncRequest(string url, string queryString, EnumHttpMode httpMode)
		{
			string str = SendSyncRequest(url, queryString, httpMode, Encoding.Default, "", "");
			return str;
		}

		public static string SendSyncRequest(string url, string queryString, Encoding contentEncoding)
		{
			string str = SendSyncRequest(url, queryString, EnumHttpMode.Get, contentEncoding, "", "");
			return str;
		}

		public static string SendSyncRequest(string url, string queryString, EnumHttpMode httpMode, Encoding contentEncoding, string certFile = "", string certPwd = "")
		{
			string end;
			char[] chrArray;
			if (!string.IsNullOrEmpty(url))
			{
				if (string.IsNullOrEmpty(queryString))
				{
					chrArray = new char[] { '?' };
					queryString = queryString.TrimStart(chrArray);
				}
				if (contentEncoding == null)
				{
					throw new ArgumentNullException("contentEncoding");
				}
				HttpWebRequest length = null;
				switch (httpMode)
				{
					case EnumHttpMode.Post:
					{
						length = (HttpWebRequest)WebRequest.Create(url);
						length.Method = "POST";
						length.ContentType = "application/x-www-form-urlencoded";
						byte[] bytes = contentEncoding.GetBytes(queryString);
						length.ContentLength = (long)((int)bytes.Length);
						Stream requestStream = length.GetRequestStream();
						requestStream.Write(bytes, 0, (int)bytes.Length);
						requestStream.Close();
						break;
					}
					case EnumHttpMode.Get:
					{
						chrArray = new char[] { '?' };
						length = (HttpWebRequest)WebRequest.Create(string.Format("{0}?{1}", url, queryString.TrimStart(chrArray)));
						length.Method = "GET";
						break;
					}
					default:
					{
						throw new ArgumentException("不支持该类型的HTTP请求模式", "httpMode");
					}
				}
				if ((string.IsNullOrEmpty(certFile) ? false : !string.IsNullOrEmpty(certPwd)))
				{
					length.ClientCertificates.Add(new X509Certificate2(certFile, certPwd));
				}
				try
				{
					end = (new StreamReader(((HttpWebResponse)length.GetResponse()).GetResponseStream(), contentEncoding)).ReadToEnd();
				}
				catch (Exception exception)
				{
					end = exception.Message;
				}
			}
			else
			{
				end = null;
			}
			return end;
		}
	}
}